<?php namespace Asset;

use Illuminate\Console\Command;

class BuildCommand extends Command {

	/**
	 * The console command name.
	 *
	 * @var string
	 */
	protected $name = 'build';
	
	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'Builds combined/minified resource files.';
	
	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
	}
	
	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function fire()
	{
        $sections = \Config::get('asset');
        
        foreach ($sections as $section => $assets) {
            foreach ($assets as $output => $input) {
            	
                // Is local, no process
                if (!is_string($output)) {
                    continue;
                }

				// Ensure $input is array
                if (!is_array($input)) {
                    $input = [$input];
                }

                 $input = join(',', $input);

                 $options = [
                     '--output' => $output,
                     'input'    => $input
                 ];

                 // by checking file extensions, it's combining/minifying as needed
                 if (ends_with($output, '.min.css')) {
                     $options['type'] = 'css';
                     $this->call('asset:minify', $options);
                 } else if (ends_with($output, '.min.js')) {
                     $options['type'] = 'js';
                     $this->call('asset:minify', $options);
                 } else {
                     $this->call('asset:combine', $options);
                 }
			}
		}
	}
}


